package com.lej.core.widget.drawable;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;

/**
 * Created by Qishui on 2015/11/24 0024.
 * 左右半圆的矩形背景Drawable
 */
public class CapsuleRoundShapeDrawable extends Drawable {
    public enum Style {
        /**
         * 实心的
         */
        SOLID,
        /**
         * 空心的
         */
        HOLLOW
    }

    private Paint mPaint;
    private Path mPath;
    private Paint.Style mPaintStyle = Paint.Style.FILL;
    private float mBorderWidth;

    public CapsuleRoundShapeDrawable() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.RED);
        mPath = new Path();
    }

    public void setPaintStyle(Style style) {
        if (style == Style.SOLID) {
            mPaintStyle = Paint.Style.FILL;
        } else if (style == Style.HOLLOW) {
            mPaintStyle = Paint.Style.STROKE;
        }
        mPaint.setStyle(mPaintStyle);
        invalidateSelf();
    }

    public void setBorderWidth(float width) {
        mBorderWidth = width;
        mPaint.setStrokeWidth(mBorderWidth);
        invalidateSelf();
    }

    @Override
    public void draw(Canvas canvas) {
        Rect bounds = getBounds();
        canvas.save();
        int radius = (int) (bounds.height() / 2.0f);
        if (mPaintStyle == Paint.Style.FILL) {
            mPath.reset();
            mPath.moveTo(radius, 0);
            mPath.lineTo(bounds.width() - radius, 0);
            mPath.arcTo(new RectF(bounds.width() - radius * 2, 0, bounds.width(), bounds.height()), -90, 180);
            mPath.lineTo(radius, bounds.height());
            mPath.arcTo(new RectF(0, 0, 2 * radius, bounds.height()), 90, 180);
            mPath.close();
        } else if (mPaintStyle == Paint.Style.STROKE) {
            int halfStrokeWidth = (int)Math.ceil(mPaint.getStrokeWidth()/2);
            mPath.reset();
            mPath.moveTo(radius, halfStrokeWidth);
            mPath.lineTo(bounds.width() - radius, halfStrokeWidth);
            mPath.arcTo(new RectF(bounds.width() - radius * 2 + halfStrokeWidth, halfStrokeWidth, bounds.width() - halfStrokeWidth, bounds.height() - halfStrokeWidth), -90, 180);
            mPath.lineTo(radius, bounds.height() - halfStrokeWidth);
            mPath.arcTo(new RectF(halfStrokeWidth, halfStrokeWidth, 2 * radius - halfStrokeWidth, bounds.height() - halfStrokeWidth), 90, 180);
            mPath.close();
        }
        canvas.drawPath(mPath, mPaint);
        canvas.restore();
    }

    @Override
    public void setAlpha(int alpha) {
        mPaint.setAlpha(alpha);
        invalidateSelf();
    }

    @Override
    public void setColorFilter(ColorFilter cf) {

    }

    @Override
    public int getOpacity() {
        return 0;
    }

//    @Override
//    public int getOpacity() {
//        return 0;
//    }

    public void setColor(int color){
        mPaint.setColor(color);
        invalidateSelf();
    }
}
